package com.example.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Box;
import com.example.tools.Result;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.domain.Product;
import com.example.service.IProductService;


/**
 * 商品信息Controller
 *
 * @author ruoyi
 * @date 2023-04-11
 */
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/system/product")
public class ProductController
{
    @Autowired
    private IProductService productService;
@Autowired
private  Box box;

    /**
     * 查询商品信息列表
     */

    @GetMapping("/list")
    public Result list(Product product)
    {

        List<Product> list = productService.selectProductList(product);
        if (list!=null)
        return Result.ok(list);
        else
            return  Result.fail().message("列表更新失败");
    }



    @GetMapping("/classify")
    public Result clas(Product product)
    {

        List<Product> list = productService.selectpro(1);
        List<Product> list1 = productService.selectpro(2);
        List<Product> list2 = productService.selectpro(3);
        box.setProducts1(list);
        box.setProducts2(list1);
        box.setProducts3(list2);


        if (box!=null)
            return Result.ok(box);
        else
            return  Result.fail().message("列表更新失败");
    }

    /**
     * 功能作用: 类目下的商品
     * 描述: 某个商品类目下的所有商品
     * 使用规则:
     * @Author: kzc
     * @Date: 2023/4/12
     */

    @GetMapping("/getProduct/{spcId}/{pageno}")
    Result getstulist(  @PathVariable("spcId") Integer spcId,
                      @ApiParam("页大小") @PathVariable("pageno") Integer pageno ) {

        Page<Product> page = new Page<>(pageno, 6);
        IPage<Product> list = productService.selectpro(page, spcId);
        if (list!=null)
            return Result.ok(list);
        else
            return  Result.fail().message("列表更新失败");

    }

    /**
     * 功能作用: https://www.uinav.com/api/public/v1/goods/qsearch?query=er
     * 描述:
     * 使用规则:
     * @Author: kzc
     * @Date: 2023/4/13
     */

    @GetMapping("/qsearch/qsearch/query/{str}")
    Result sdf( @PathVariable("str") String str) {

        Product p=new Product();
        p.setSpName(str);
        p.setSpParticulars(str);
        List<Product> products = productService.selectProductList(p);
        return Result.ok(products);
    }

/**
* 功能作用: http://127.0.0.1:3000/api/index_list/1/data
* 描述:
* 使用规则:
* @Author: kzc
* @Date: 2023/4/13
*/
@GetMapping("/classify/index_list/{id}/data/{pageno}")
public Result getcla(@PathVariable(value = "id",required = false) Integer id,
                    @PathVariable("pageno") Integer pageno)
{
    Page<Product> page=new Page<>(pageno,6);


    IPage<Product> list = productService.selectpro(page, id);

    return Result.ok(list);
}

    @GetMapping("/classify/index_list/data/{pageno}")
    public Result getdata( @PathVariable("pageno") Integer pageno)
    {
        Page<Product> page=new Page<>(pageno,3);
        IPage<Product> list = productService.selectpro(page, 21);

        return Result.ok(list);
    }

/**
* 功能作用: 商品详情
* 描述:
* 使用规则:
* @Author: kzc
* @Date: 2023/4/21
*/


    @GetMapping("/detail/{spid}")
    public Result selelist( @PathVariable("spid") Integer spid)
    {

        Product seletlist = productService.seletlist(spid);
//        String ur1=seletlist.getProsp().getProductimgUrl1();
//        seletlist.getImglist().add(seletlist.getProsp().getProductimgUrl1());
//        seletlist.getImglist().add(seletlist.getProsp().getProductimgUrl2());
//        seletlist.getImglist().add(seletlist.getProsp().getProductimgUrl3());
//        seletlist.getImglist().add(seletlist.getProsp().getProductimgUrl4());
//        seletlist.getImglist().add(seletlist.getProsp().getProductimgUrl5());
        Map<String,Object> map=new HashMap<>();


        List<String> list=new ArrayList<>();
        list.add(seletlist.getProsp().getProductimgUrl1());
        list.add(seletlist.getProsp().getProductimgUrl2());
        list.add(seletlist.getProsp().getProductimgUrl3());
        list.add(seletlist.getProsp().getProductimgUrl4());
        list.add(seletlist.getProsp().getProductimgUrl5());
        seletlist.setImglist(list);

        return Result.ok(seletlist);
    }
/**
* 功能作用: 添加商品
* 描述:
* 使用规则:
* @Author: kzc
* @Date: 2023/4/23
*/

    @PostMapping("/detail/add")
    public Result selelist( @RequestBody Product product){
        if (product!=null){
            product.setCreatTime(new Date());
            int i = productService.insertProduct(product);
            if (i!=0){
                return  Result.fail().message("添加失败");
            }
            else
                return Result.ok("添加成功");
        }
        return Result.ok("添加成功");
    }



//    /**
//     * 导出商品信息列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:product:export')")
//    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Product product)
//    {
//        List<Product> list = productService.selectProductList(product);
//        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
//        util.exportExcel(response, list, "商品信息数据");
//    }
//
//    /**
//     * 获取商品信息详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('system:product:query')")
//    @GetMapping(value = "/{spId}")
//    public AjaxResult getInfo(@PathVariable("spId") Integer spId)
//    {
//        return success(productService.selectProductBySpId(spId));
//    }
//
//    /**
//     * 新增商品信息
//     */
//    @PreAuthorize("@ss.hasPermi('system:product:add')")
//    @Log(title = "商品信息", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody Product product)
//    {
//        return toAjax(productService.insertProduct(product));
//    }
//
//    /**
//     * 修改商品信息
//     */
//    @PreAuthorize("@ss.hasPermi('system:product:edit')")
//    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody Product product)
//    {
//        return toAjax(productService.updateProduct(product));
//    }
//
//    /**
//     * 删除商品信息
//     */
//    @PreAuthorize("@ss.hasPermi('system:product:remove')")
//    @Log(title = "商品信息", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{spIds}")
//    public AjaxResult remove(@PathVariable Integer[] spIds)
//    {
//        return toAjax(productService.deleteProductBySpIds(spIds));
//    }
}
